<?php
/**
 * @copyright meila.com
 * @author shaopu@
 * @name 
 * @param 
 *         $xxx = 作用
 * @static 
 *         XXX = 作用
 * 
 * 
 */
$dir = dirname(dirname(__FILE__));
include($dir.'/__queue_global.php');

class ml_mq_add_atme extends MqClass{
    const QUEUE_NAME = ML_QUEUENAME_ADD_ATME;
    private $oAtme ;
    private $oAct2atme ;
    private $oComment;
    private $oRdsNotice;
    

    public function __construct($queue)
    {
        $this->oAtme = new ml_model_dbAtMe();
        $this->oComment = new ml_model_dbComment();
        $this->oAct2atme = new ml_model_dbAct2atme();
        $this->oRdsNotice = new ml_model_rdsNotice();
        
        parent::__construct($queue);
    }
    /**
     * 内容中@
     * array(
     *         type = 1
     *         'act_uid'
     *         'rid'
     *         'dest_uids' = array()
     * )
     * 
     *
     * @return unknown
     */
    public function run_job(){
        //接收的数据
        $arr = $this->src_data;
        $type = $arr['type'];
        
        switch ($type)
        {
            case ML_ATME_TYPE_IN_CONTENT:
                $this->_add_content_atme($arr);break;
            case ML_ATME_TYPE_IN_COLLECTION:
                $this->_add_be_collected_atme($arr);break;
            case ML_ATME_TYPE_IN_NEWCOMMENT:
                $this->_add_new_comment_atme($arr);break;
            case ML_ATME_TYPE_IN_COLLECTION:
                $this->_add_reply_comment_atme($arr);break;
            default:break;
        }
        
        return true;
    }

    private function _add_content_atme($data)
    {
        $act_uid = $data['act_uid'];
        $act_rid = $data['rid'];
        $aAtUids = $data['dest_uids'];
        
        //取内容信息
        $ctime = date('Y-m-d H:i:s');
        /**
         * @todo 
         */
        
        $aInsert = array(
            'act_uid' => $act_uid,
            'atme_info' => array(
                'act_rid' => $act_rid
            ),
            'from' => ML_FROM_WEB,
        );
        $aAtmeRid = array();
        foreach ($aAtUids as $uid)
        {
            //自己@自己不提示
            if($uid == $act_uid)
                continue;
                
            $rs = $this->oAtme->add_atme($uid , ML_ATME_TYPE_IN_CONTENT , $aInsert ,$ctime, $atme_rid);
            /**
             * @todo 
             */
            $aAtmeRid[] = $atme_rid;
            $rs = $this->oRdsNotice->add_notice($uid , ml_model_rdsNotice::NTCTYPE_ADDATME );
            ml_tool_sendMq::add_atnick_latestAt($act_uid, $uid);
        }
        $rs = $this->oAct2atme->add_act2atme($act_rid , $act_uid , $aAtmeRid);
        /**
         * @todo 
         */
        
        
        return ;
    }
    private function _add_be_collected_atme($data)
    {
        $act_uid = $data['act_uid'];
        $dest_rid = $data['rid'];
        $col_id = $data['col_id'];
        $dest_uid = ml_tool_resid::resid2uid($dest_rid);
        $act_rid = ml_tool_resid::make_resid($act_uid , ML_RESID_TYPE_COLLECT , $col_id);
        
        if($act_uid == $dest_uid)
            return ;
        
        $aInsert = array(
            'act_uid' => $act_uid,
            
            'atme_info' => array(
                'dest_rid' => $dest_rid
            ),
            'from' => ML_FROM_WEB
        );
        $ctime = date('Y-m-d H:i:s');
        $rs = $this->oAtme->add_atme($dest_uid , ML_ATME_TYPE_IN_COLLECTION , $aInsert ,$ctime, $atme_rid);
        $rs = $this->oRdsNotice->add_notice($dest_uid , ml_model_rdsNotice::NTCTYPE_ADDATME );
        $rs = $this->oAct2atme->add_act2atme($act_rid , $act_uid , array($atme_rid));
        return ;
    }
    private function _add_new_comment_atme($data)
    {
        $act_uid = $data['act_uid'];
        $cmt_id = $data['cmt_id'];
        $cmt_rid = ml_tool_resid::make_resid($act_uid , ML_RESID_TYPE_COMMENT , $cmt_id);
        $src_rid = $data['rid'];
        $aAtUids = $data['at_uids'];
        $src_uid = ml_tool_resid::resid2uid($src_rid);
        
        $rs = $this->oComment->getCommentById($src_uid , $cmt_id);
        $aCmt = $this->oComment->get_data();
        
        $aInsert = array(
            'act_uid' => $act_uid,
            'content' => $aCmt['content'],
            'atme_info' => array(
                'cmt_rid' => $cmt_rid,
                'src_rid' => $src_rid
            ),
            'from' => ML_FROM_WEB
        );
        
        $aAtmeRid = array();
        $ctime = $aCmt['ctime'];
        foreach ($aAtUids as $dest_uid)
        {
            //自己@自己不提示
            if($dest_uid == $act_uid)
                continue;
            
            $rs = $this->oAtme->add_atme($dest_uid , ML_ATME_TYPE_IN_NEWCOMMENT , $aInsert ,$ctime, $atme_rid);
            $aAtmeRid[] = $atme_rid;
            $rs = $this->oRdsNotice->add_notice($dest_uid , ml_model_rdsNotice::NTCTYPE_ADDATME );
            ml_tool_sendMq::add_atnick_latestAt($act_uid, $dest_uid);
        }
        $rs = $this->oAct2atme->add_act2atme($cmt_rid , $act_uid , $aAtmeRid);
        return ;
    }
    private function _add_reply_comment_atme($data)
    {
        $act_uid = $data['act_uid'];
        $cmt_id = $data['cmt_id'];
        $act_rid = ml_tool_resid::make_resid($act_uid , ML_RESID_TYPE_COMMENT , $cmt_id);
        $old_cmt_id = $data['old_cmt_id'];
        
        $src_rid = $data['rid'];
        $dest_uid = $data['dest_uid'];
        $src_uid = ml_tool_resid::resid2uid($src_rid);
        
        $rs = $this->oComment->getCommentById($src_uid , $cmt_id);
        $aCmt = $this->oComment->get_data();
        
        $rs = $this->oComment->getCommentById($src_uid , $old_cmt_id);
        $aOldCmt = $this->oComment->get_data();
        
        $content = $aCmt['content'].' //'.Tool_string::substr_by_width($aOldCmt['content'] , 0 , 20);
        $aInsert = array(
            'act_uid' => $act_uid,
            'content' => $aCmt['content'],
            'atme_info' => array(
                'cmt_rid' => $cmt_rid,
                'src_rid' => $src_rid
            ),
            'from' => ML_FROM_WEB
        );
        $ctime = $aCmt['ctime'];
        $rs = $this->oAtme->add_atme($dest_uid , ML_ATME_TYPE_IN_REPLYCOMMENT , $aInsert ,$ctime, $atme_rid);
        $rs = $this->oRdsNotice->add_notice($dest_uid , ml_model_rdsNotice::NTCTYPE_ADDATME );
        $rs = $this->oAct2atme->add_act2atme($dest_rid , $act_uid , array($atme_rid));
        return ;
    }
}

ml_run_queue_check(ml_mq_add_atme::QUEUE_NAME);

$conf_array = ml_factory::load_standard_conf('redis');
$conf_array = $conf_array['meila_queue'];
$xblog_obj = new ml_mq_add_atme(new RsQueue(ml_mq_add_atme::QUEUE_NAME));
$argv[1]   = __FILE__;
$xblog_obj->setArgv($argv[1]);
$xblog_obj->execute();
?>